package cn.com.egova.materialdesignexample.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.egova.materialdesignexample.R;

import static android.support.design.widget.Snackbar.make;

public class SnackbarActivity extends AppCompatActivity {
    private CoordinatorLayout mCoordinatorLayout;
    private Snackbar mSnackbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_snackbar);

        initToolbar();

        initView();
    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("Snackbar");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setNavigationIcon(R.mipmap.icon_back);
            toolbar.setNavigationOnClickListener(new NavigationView.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
    }

    private void initView() {
        mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_snackbar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.noAction:
                showSnackbarWithNoAction();
                break;
            case R.id.hasAction:
                showSnackbarWithAction();
                break;
            case R.id.addImage:
                addView();
                break;
            case R.id.changePosition:
                changePosition();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    private void showSnackbarWithNoAction() {
        make(mCoordinatorLayout, "show snackbar without action", Snackbar.LENGTH_LONG).show();
    }

    private void showSnackbarWithAction() {
//        Snackbar.make(mCoordinatorLayout, "show snackbar with action", Snackbar.LENGTH_LONG)
//                .setAction("Action", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Log.i("TAG",v.toString());
//                        Snackbar.make(mCoordinatorLayout, "ActionClick!", Snackbar.LENGTH_LONG).show();
//                    }
//                }).show();

        //获取Snackbar的实例.
        mSnackbar = make(mCoordinatorLayout,"show snackbar with action",Snackbar.LENGTH_LONG)
            .setAction("Action", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //获取SnackbarLayout的实例,它继承自LinearLayout,Snackbar的内容都放在里边.
                    View view = mSnackbar.getView();
                    TextView tv_msg = (TextView) view.findViewById(R.id.snackbar_text);
                    TextView tv_action = (TextView) view.findViewById(R.id.snackbar_action);
                    tv_msg.setTextColor(Color.YELLOW);  //设置Snackbar的消息文字的颜色.
                    tv_action.setTextColor(Color.GREEN); //设置Snackbar上Action的文字颜色(方式一).
                }
            });
        mSnackbar.setActionTextColor(Color.RED);   //设置Snackbar上Action的文字颜色(方式2).

        //设置Snackbar监听
        mSnackbar.setCallback(new Snackbar.Callback() {
            @Override
            public void onDismissed(Snackbar snackbar, int event) {
                super.onDismissed(snackbar, event);  //super里是空实现.
                Log.i("TAG","snackbar is dismissed");
            }

            @Override
            public void onShown(Snackbar snackbar) {
                super.onShown(snackbar);  //super里是空实现.
                Log.i("TAG","snackbar is shown");
            }
        });

        mSnackbar.show();  //显示在窗体上.
    }

    /**
     * 在SnackBar中添加图片控件.
     */
    private void addView(){
        Snackbar snackbar = Snackbar.make(mCoordinatorLayout, "show snackbar add view", Snackbar.LENGTH_LONG);
        View snackbarView = snackbar.getView();
        //设置icon
        ImageView iconImage = new ImageView(this);
        iconImage.setImageResource(R.mipmap.ic_launcher);
        //icon插入布局
        Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) snackbarView;
        Snackbar.SnackbarLayout.LayoutParams params = new Snackbar.SnackbarLayout.LayoutParams(Snackbar.SnackbarLayout.LayoutParams.WRAP_CONTENT, Snackbar.SnackbarLayout.LayoutParams.WRAP_CONTENT);
        //让icon的布局位于父布局垂直居中的位置
        params.gravity = Gravity.CENTER_VERTICAL;
        iconImage.setLayoutParams(params);
        snackbarLayout.addView(iconImage, 0);

        snackbar.show();
    }


    /**
     * 改变snackbar在容器中的位置.
     */
    private void changePosition(){
        Snackbar snackbar = Snackbar.make(mCoordinatorLayout, "show snackbar change position", Snackbar.LENGTH_LONG);
        // 获取 snackbar 视图
        View snackbarView = snackbar.getView();
        ViewGroup.LayoutParams vl = snackbarView.getLayoutParams();
        CoordinatorLayout.LayoutParams cl = new CoordinatorLayout.LayoutParams(vl.width, vl.height);
        cl.gravity = Gravity.CENTER_VERTICAL;
        snackbarView.setLayoutParams(cl);

        snackbar.show();
    }

}
